package com.joint.base.dao.impl;



import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.util.LogUtil;
import com.joint.base.bean.FlowEnum;
import com.joint.base.dao.TaskEntityDao;
import com.joint.base.entity.TaskEntity;
import com.joint.base.entity.Users;
import com.joint.base.util.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;

@Repository
public class TaskEntityDaoImpl extends BaseEntityDaoImpl<TaskEntity, String> implements TaskEntityDao {


    @Override
    public List<TaskEntity> getInfoByEntityName(String entityName) {
        Assert.notNull(entityName, "entityName is required");
        String hql = "from TaskEntity where entityName = :entityName and state = :state";
        List<TaskEntity> taskEntityList = getSession().createQuery(hql).setParameter("entityName", entityName)
                .setParameter("state", BaseEnum.StateEnum.Enable).list();
        return taskEntityList;
    }

    @Override
    public TaskEntity getInfoByKeyId(String keyId) {
         Assert.notNull(keyId, "keyId is required");
         String hql = "from TaskEntity where keyId = :keyId and state = :state";
         List<TaskEntity> taskEntityList = getSession().createQuery(hql).setParameter("keyId", keyId)
                .setParameter("state", BaseEnum.StateEnum.Enable).list();
         if(taskEntityList.size()>0){
             return taskEntityList.get(0);
         }
         return null;
    }

    @Override
    public List<TaskEntity> getInfoByReaders(String entityName, FlowEnum.ProcessState[] processState, String users_id) {
        Assert.notNull(entityName, "entityName is required");
        Assert.notNull(processState, "processState is required");
        Assert.notNull(users_id, "users_id is required");
        String processStateList = "";
        for(FlowEnum.ProcessState p:processState){
            processStateList += "\""+p.name()+"\",";
        }
        if(StringUtils.isNotEmpty(processStateList)){
            processStateList = processStateList.substring(0,processStateList.length()-1);
        }
        String sql = "select a.* from sys_taskentity a left join sys_taskentity_readers b on a.id = b.id ";
        sql += " where a.entityName = \""+  entityName + "\" and a.processState in (" +  processStateList+ ") " ;
        sql += " and a.state = \"Enable\" and b.readersId = \""+  users_id + "\" ";
      //  LogUtil.info("sql:" +sql);
        List<TaskEntity> taskEntityList = getSession().createSQLQuery(sql).addEntity(TaskEntity.class).list();
        return taskEntityList;
    }

    @Override
    public List<TaskEntity> getInfoByDraft(FlowEnum.ProcessState processState, Users users) {
        Assert.notNull(processState, "processState is required");
        Assert.notNull(users, "users is required");
        String hql = "from TaskEntity where processState = :processState and creater = :users and state = :state";
        List<TaskEntity> taskEntityList = getSession().createQuery(hql).setParameter("processState", processState)
                .setParameter("users", users).setParameter("state", BaseEnum.StateEnum.Enable).list();
        return taskEntityList;
    }

    @Override
    public List<TaskEntity> getInfoByEditors( FlowEnum.ProcessState[] processState, String users_id) {

        Assert.notNull(processState, "processState is required");
        Assert.notNull(users_id, "users_id is required");
        String processStateList = "";
        for(FlowEnum.ProcessState p:processState){
            processStateList += "\""+p.name()+"\",";
        }
        if(StringUtils.isNotEmpty(processStateList)){
            processStateList = processStateList.substring(0,processStateList.length()-1);
        }
        String sql = "select a.* from sys_taskentity a left join sys_taskentity_editors b on a.id = b.id ";
        sql += " where  a.processState in (" +  processStateList+ ") " ;
        sql += " and a.state = \"Enable\" and b.editorsId = \""+  users_id + "\" ";
        //  LogUtil.info("sql:" +sql);
        List<TaskEntity> taskEntityList = getSession().createSQLQuery(sql).addEntity(TaskEntity.class).list();
        return taskEntityList;
    }
}